//index.js
//获取应用实例
var WxSearch = require('../../utils/wxSearch_module.js')
var app = getApp()
Page({
  data: {
    isStartPos:true,
    queryLocations:[],
    selectedLocation:null,
    wxSearchData: {
      view: {
        isShow: false,
        mindKeys: null,
        isShowSearchKey: false,
        isShowMap:false,
        isShowSearchHistory: true
      }
    }
  },
  onLoad: function (options) {
    var that = this
    that.setData({isStartPos: options.isStartPos })
    console.log(options.isStartPos)
    //初始化的时候渲染wxSearchdata
    WxSearch.init(that, 43, [], that.data.wxSearchData.view.isShowSearchKey, that.data.wxSearchData.view.isShowSearchHistory);
    WxSearch.initMindKeys([]);
    if(options.isStartPos=="true"){
      getApp().globalData.qqmapsdk.reverseGeocoder({
        location: {
          latitude: getApp().globalData.location.latitude,
          longitude: getApp().globalData.location.longitude
        },
        success: function (res) {
          console.log(res);
          const address = res.result.address
          getApp().globalData.address = address
          console.log(res.result.address);
          that.setData({
            wxSearchData: {
              view: {
                isShow: true,
                mindKeys: null,
                isShowSearchKey: false,
                isShowMap:true,
                isShowSearchHistory: true,
              },
              value:res.result.address,
              
            },
            location:{
              latitude: getApp().globalData.location.latitude,
              longitude: getApp().globalData.location.longitude
            },
            //需要手动设置逆解析的地址成一个selectedLocation
            selectedLocation:{
              id:null,
              title:res.result.address,
              address:res.result.address,
              category:null,
              type:null,
              location:{
                lat:getApp().globalData.location.latitude,
                lng:getApp().globalData.location.longitude
              },
              adcode:res.result.ad_info.adcode,
              province:res.result.address_component.province,
              city:res.result.address_component.city,
              district:res.result.address_component.district,
            }
          })
        },
        fail: function (res) {
          console.log(res);
        }
      })
    }else{
      that.setData({
        wxSearchData: {
          view: {
            isShow: true,
            mindKeys: null,
            isShowSearchKey: false,
            isShowMap:false,
            isShowSearchHistory: true,
          },
          value:"",
        }
      })
    }
  },
  // wxSearchFn: function (e) {
  //   var that = this
  //   WxSearch.wxSearchAddHisKey(that);
  //   console.log(e)
  // },
  wxSearchInput: function (e) {
    var that = this
    this.data.queryLocations=[]
    console.log("Searching " + e.detail.value)
    getApp().globalData.qqmapsdk.getSuggestion({
      keyword: e.detail.value,
      region: getApp().globalData.city,
      success: function (res) {
        var targets=new Array()
        for (let i = 0; i < res.data.length; i++) {
            console.log("data="+res.data[i]);
            targets.push(res.data[i].title)
            that.data.queryLocations[res.data[i].title] = res.data[i]
        }
        console.log(targets)
        WxSearch.initMindKeys(targets)
        //不断的setData可能造成打字渲染的问题
        WxSearch.wxSearchInput(e, that);
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    })
    //下面的这个放这里就会有时候有延迟
    //WxSearch.wxSearchInput(e, that);
  },
  wxSearchFocus: function (e) {
    var that = this
    WxSearch.wxSearchFocus(e, that);
  },
  wxSearchBlur: function (e) {
    var that = this
    //WxSearch.wxSearchBlur(e, that);
  },
  //选择提示词后回调
  wxSearchKeyTap: function (e) {
    var that = this
    this.data.selectedLocation = this.data.queryLocations[e.target.dataset.key]
    console.log(this.data.selectedLocation)
    this.setData({
      location:{
        latitude:this.data.selectedLocation.location.lat,
        longitude:this.data.selectedLocation.location.lng
      }
    })
    WxSearch.wxSearchKeyTap(e, that);
  },
  wxSearchDeleteKey: function (e) {
    var that = this
    WxSearch.wxSearchDeleteKey(e, that);
  },
  wxSearchDeleteAll: function (e) {
    var that = this;
    WxSearch.wxSearchDeleteAll(that);
  },
  wxSearchTap: function (e) {
    var that = this
    WxSearch.wxSearchHiddenPancel(that);
    console.log(e)
  },
  confirm: function (event) {
    console.log(event)
    //WxSearch.wxSearchAddHisKey(this);
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length - 2];//上一页面
    var data={}
    console.log("即将返回")
    console.log(this.data.selectedLocation)
    if (this.data.isStartPos=='true'){
      data = { startLocation: this.data.selectedLocation}
    }else{
      data = { endLocation: this.data.selectedLocation}
    }
    //直接给上一页面赋值
    prevPage.setData(data);
    wx.navigateBack({
      delta: 1
    })
  }
})